﻿'   
'
'   g10EXETryItVB -               Copyright (C) 2011 PH Informática, SA.
'
'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the  Free Software Foundation, either version 3 of the  License,  or
'   (at your option) any later version.
'
'   This  program  is distributed in  the hope that it will  be  useful,
'   but  WITHOUT  ANY  WARRANTY; without even the  implied  warranty  of
'   MERCHANTABILITY  or  FITNESS  FOR  A  PARTICULAR  PURPOSE.  See  the
'   GNU General Public License for more details.
'
'   You  should have received  a copy of  the GNU General Public License
'   along with this program. If not, see <http://www.gnu.org/licenses/>.
'
'

Public Class frmMapa

    ' Flag de PAN
    Private onDrag As Boolean = False

    ' Objectos de auxílio a "movimentações" da imagem do mapa
    Private dMapCenterX As Double = -35000
    Private dMapCenterY As Double = 160000
    Private lMapScale As Long = 100000

    Private oAuxBitmap As Bitmap
    Private oPonto As g10WSTryIt.g10GeoPoint

    Private Sub frmMapa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ToolStripStatusLabel1.Text = "A carregar mapa inicial ..."
        Application.DoEvents()

        ' obter mapa
        Dim oImage As System.Drawing.Image
        oImage = GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height)

        If oImage IsNot Nothing Then
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            PictureBox1.Image = oImage
        End If

        ToolStripStatusLabel1.Text = "A carregar lista de layers ..."
        Application.DoEvents()

        Dim oLayers As New g10WSTryIt.g10MapLayerSet
        oLayers = myWs.GetLayers(sApiTokenID, sSessionID)

        bLoading = True

        ' carregar Layers
        TreeView1.Nodes.Clear()
        If oLayers IsNot Nothing Then
            For Each l As g10WSTryIt.g10MapguideLayer In oLayers.Layers
                TreeView1.Nodes.Add(l.Name, l.LegendLabel).Checked = l.GetVisibility
            Next
        End If

        ' apenas concelhos do distrito do porto
        LoadConcelhos("cc0d86be-352a-43e8-b77b-9d370c079471")

        bLoading = False

        ToolStripStatusLabel1.Text = "OK!"
        Application.DoEvents()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ToolStripStatusLabel1.Text = "A obter mapa ..."
        Application.DoEvents()

        ' Zoom menos
        lMapScale *= 2
        PictureBox1.Image = GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height)

        ToolStripStatusLabel1.Text = ""
        Application.DoEvents()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ToolStripStatusLabel1.Text = "A obter mapa ..."
        Application.DoEvents()

        ' Zoom mais
        lMapScale /= 2
        PictureBox1.Image = GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height)

        ToolStripStatusLabel1.Text = ""
        Application.DoEvents()

    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        If Not onDrag Then

            onDrag = True
            oAuxBitmap = New Bitmap(PictureBox1.Image)

            oPonto = New g10WSTryIt.g10GeoPoint
            oPonto.X = e.X
            oPonto.Y = e.Y

        End If

    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

        If onDrag Then

            Dim midX As Double = sender.width / 2 + (oPonto.X - e.X)
            Dim midY As Double = sender.height / 2 + (oPonto.Y - e.Y)

            Dim newHW As Integer = Math.Max(sender.width, sender.height)

            Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
            g.Clear(Color.Beige)

            Dim destRect As Rectangle = New Rectangle(0, 0, sender.width, sender.height)
            Dim srcRect As Rectangle = New Rectangle(midX - newHW / 2, midY - newHW / 2, newHW, newHW)

            g.DrawImage(oAuxBitmap, destRect, srcRect, GraphicsUnit.Pixel)
            PictureBox1.Invalidate()

            Application.DoEvents()

        End If

    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp

        If onDrag Then

            onDrag = False

            Dim pci As New g10WSTryIt.g10GeoPoint
            pci.X = sender.width / 2 - (e.X - oPonto.X)
            pci.Y = sender.height / 2 - (e.Y - oPonto.Y)

            Dim pWorld As New g10WSTryIt.g10GeoPoint
            pWorld = myWs.GetActualViewCoordsImage2Map(sApiTokenID, sSessionID, pci)

            dMapCenterX = pWorld.X
            dMapCenterY = pWorld.Y

            ToolStripStatusLabel1.Text = "A obter mapa ..."
            Application.DoEvents()

            PictureBox1.Image = GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height)

            ToolStripStatusLabel1.Text = ""
            Application.DoEvents()

        End If

    End Sub

    Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck

        If Not bLoading Then
            myWs.SetLayerVisibility(sApiTokenID, sSessionID, e.Node.Name, e.Node.Checked)

            ToolStripStatusLabel1.Text = "A obter mapa ..."
            Application.DoEvents()

            PictureBox1.Image = GetMapa(dMapCenterX, dMapCenterY, lMapScale, PictureBox1.Width, PictureBox1.Height)

            ToolStripStatusLabel1.Text = ""
            Application.DoEvents()
        End If

    End Sub

    Private Sub LoadConcelhos(ByVal distritoid As String)

        ToolStripStatusLabel1.Text = "A carregar lista de concelhos ..."
        Application.DoEvents()

        ListBox2.Items.Clear()
        ListBox3.Items.Clear()

        Dim concelhos As g10WSTryIt.Entidades
        concelhos = myWs.getConcelhos(sApiTokenID, sSessionID, distritoid)

        If concelhos IsNot Nothing Then
            If concelhos.Entidades1 IsNot Nothing Then
                For Each c As g10WSTryIt.Entidade In concelhos.Entidades1
                    ListBox2.Items.Add(New ListItem(c.Nome, c.Id, c))
                Next
            End If
        End If

        ToolStripStatusLabel1.Text = ""
        Application.DoEvents()

    End Sub

    Private Sub LoadFreguesias(ByVal concelhoid As String)

        ToolStripStatusLabel1.Text = "A carregar lista de freguesias ..."
        Application.DoEvents()

        ListBox3.Items.Clear()

        Dim freguesias As g10WSTryIt.Entidades
        freguesias = myWs.getFreguesias(sApiTokenID, sSessionID, concelhoid)

        If freguesias IsNot Nothing Then
            If freguesias.Entidades1 IsNot Nothing Then
                For Each f As g10WSTryIt.Entidade In freguesias.Entidades1
                    ListBox3.Items.Add(New ListItem(f.Nome, f.Id, f))
                Next
            End If
        End If

        ToolStripStatusLabel1.Text = ""
        Application.DoEvents()

    End Sub

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged

        ToolStripStatusLabel1.Text = "A localizar concelho ..."
        Application.DoEvents()

        Dim idConc As String = CType(CType(ListBox2.SelectedItem, ListItem).Object, g10WSTryIt.Entidade).Id

        LoadFreguesias(idConc)

        Dim cent As g10WSTryIt.g10GeoPoint
        cent = myWs.getGeometryCentroidById(sApiTokenID, sSessionID, idConc)

        myWs.SetLayerVisibility(sApiTokenID, sSessionID, "freguesias", False)
        myWs.SetLayerVisibility(sApiTokenID, sSessionID, "concelhos", True)
        myWs.SetLayerVisibility(sApiTokenID, sSessionID, "distritos", False)

        lMapScale = 200000

        ToolStripStatusLabel1.Text = "A obter mapa ..."
        Application.DoEvents()

        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        PictureBox1.Image = GetMapa(cent.X, cent.Y, lMapScale, PictureBox1.Width, PictureBox1.Height)

        dMapCenterX = cent.X
        dMapCenterY = cent.Y

        ToolStripStatusLabel1.Text = ""
        Application.DoEvents()

    End Sub

    Private Sub ListBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox3.SelectedIndexChanged

        ToolStripStatusLabel1.Text = "A localizar freguesia ..."
        Application.DoEvents()

        Dim idFreg As String = CType(CType(ListBox3.SelectedItem, ListItem).Object, g10WSTryIt.Entidade).Id

        Dim cent As g10WSTryIt.g10GeoPoint
        cent = myWs.getGeometryCentroidById(sApiTokenID, sSessionID, idFreg)

        myWs.SetLayerVisibility(sApiTokenID, sSessionID, "freguesias", True)
        myWs.SetLayerVisibility(sApiTokenID, sSessionID, "concelhos", False)
        myWs.SetLayerVisibility(sApiTokenID, sSessionID, "distritos", False)

        lMapScale = 50000

        ToolStripStatusLabel1.Text = "A obter mapa ..."
        Application.DoEvents()

        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        PictureBox1.Image = GetMapa(cent.X, cent.Y, lMapScale, PictureBox1.Width, PictureBox1.Height)

        dMapCenterX = cent.X
        dMapCenterY = cent.Y

        ToolStripStatusLabel1.Text = ""
        Application.DoEvents()

    End Sub

End Class